Practical 3 - Full Version

Table of Contents

1. Advanced Variables
2. Grass Grows...
3. Giving Birth.
4. Creating a graph.
print

Practical 3 - An Advanced NetLogo Model


In the previous practical session, you created a full (albeit simple) NetLogo model of some sheep eating grass. In this practical, we will improve the realism of the model and add some new functionality:

  1. Tracking the time since a sheep's last meal and killing sheep who starve.
  2. Healthy sheep can give birth to new sheep
  3. Having grass take time to re-grow after being eaten
  4. A graph of the number of living sheep

Before getting started, make a copy of the NetLogo model that you made for practical 2. In this practical we will improve the realism of that simple model, but it is worth copying it in case you want to go back to the original at any time. The easiest way to do this is to simply copy the file using Windows Explorer.

When you have done that, move on to the first part to begin.

The final model.

Practical 3, part 1 - Advanced Variables


Recall from practical 1 that variables can be used to hold information about different objects in NetLogo. For example, turtles might have a variable called age that saves the age of each turtle. Similarly, patches might have a variable called grain that stores (for example) the amount of grain growing on each patch.

Contexts and variables

We have already made use of some turtle and patch variables, such as color, xcor and ycor (for turtles), or pcolor for patches. These are build in to NetLogo; you can see a full list of built in variables here:

http://ccl.northwestern.edu/netlogo/docs/dictionary.html#builtinvariables

However, to build more interesting models we need to be able to create new variables to store different pieces of information that might be required for the system under investigation. This part of the practical will look at how to do this.

  1. You should have made a copy of the simple model you created for practical 2. Open this now; we will add new functionality to this model.
  2. We want to create two new variables. One, called energy, will store the amount of life energy each turtle has. If this reaches 0 then they die. The second variable, time-since-eaten, will store the amount of time that has passed since a patch of grass has been eaten. Grass takes some time to regrow, so this variable can tell us when a patch has completely regrown.

  3. To create these new variables, add the following code right at the top of the model (before the to setup line). turtles-own [
     energy
    ]

    patches-own [
     time-since-eaten
    ]
  4. In the code above, turtles-own tells NetLogo that we are going to define some new variables that will belong to the turtles. Variables can then be created in between the square brackets (one per line). We create a single variable called energy. Similarly, the patches-own code means that we are about to define variables for the patches.

    Code for using the 'energy' variable

    We will make use of the energy variable right away.

  5. Add the following line to the setup-turtles procedure, just after the ask turtles [ line: set energy 5 This will give each turtle 5 units of energy at the beginning of the model. (See the right image if you're not sure exactly where the new code should go).
  6. Also add the following lines to the go procedure, just after the ask turtles [ line: set energy ( energy - 1 )
    if energy <= 0[
     die
    ]
    The first line reduces the value of the energy variable by one. So, each time the model iterates every turtle will lose one unit of energy. The next line (if energy <= 0[) checks to see if the turtle has lost all its energy. If it has, then the die command removes the turtle from the model. (If you're confused about the 'if' statement, refer to the lecture ).
  7. You'll notice that in the image of the code above, some of the text is grey. In NetLogo, anything that appears after a ';' symbol is called a comment. These aren't actually read by NetLogo - they can be used by the developer to explain what it is that their code is doing. Always include comments in your code! These aren't just so that other people can understand your code, they are also so that you can remember what your code does in case you forget.

    Anyway, now the turtles have some energy when they are created and each time the model iterates they lose some. To conclude this part of the practical, we will add some code to make the agents eat grass and gain energy.

    Code to eat grass
  8. In the 'go' procedure, find the code that makes the turtles eat the grass: if pcolor = green [
     set pcolor brown
    ]
    All that code does is change the colour of the patch that the turtle is standing on. Add two new lines so that the code now reads: if pcolor = green [
     set pcolor brown
     set energy (energy + 5 )
     set time-since-eaten 0
    ]
    The new code still changes the colour of the patch from green to brown to indicate that it has been eaten. But now it also gives the turtle five units of energy and sets the time-since-eaten variable to 0 to indicate that no time has elapsed since the patch was eaten. (This will be important later when we want grass to grow back slowly over time).
  9. Image showing all turtles have died

    Note: the more keen-eyed of you will have noticed that time-since-eaten is a variable that belongs to the patches, so we shouldn't be able to change it from within the turtle context (i.e. in an ask turtles [ ... ] block). Remember we can get around this because NetLogo lets you access patch variables from the turtle context for the patch that a turtle is currently standing on. This is quite important; if you're confused either ask a demonstrator or refer back to the variables diagram from the lecture.

  10. Finally, check that everything works by clicking on the 'interface' tab and running the model. If it runs too quickly for you to see what is happening use the control at the top to slow it down. You should see the sheep die. This is because we have given them the ability to eat grass, but we haven't given the grass the ability to grow back! We'll do this next.

Have a go at the questions below and make sure you're comfortable with the answers, then move on to part 2.

Questions

  1. List some of the built-in variables you have come across that belong to turtles.  
  2. Also list some for patches.  
  3. What is the URL for the NetLogo documentation that shows you all the built-in variables that belong to patches and turtles.  
  4. Write the NetLogo code that you would use to increase the value of a variable called happiness by ten units.  
  5. Write the NetLogo code that you would use to test if the value of a variable called deprivation is greater or equal to twenty and, if it is greater, set it to zero:  
     
     

Practical 3, part 2 - Grass Grows..


grass-regrow-time slider

This part is very short - all you need to do is add a few lines so that the grass in the model re-grows after a certain amount of time. Importantly, we are going to create a slider so that the person running the model can experiment with different regrowth times.

  1. To begin with, create a slider called grass-regrow-time (if you can't remember how to create sliders, have a look back at practical 2). Give the slider a range of 0 to 100 with a default value of your choice (I chose 80).
  2. Code for re-growing grass

    Recall that, in NetLogo, sliders represent variables. So, if you have created a slider called grass-regrow-time, NetLogo also makes a variable called grass-regrow-time that you can refer to in your model code. The variable will be part of the observer context, so all turtles and patches can see what value it holds. We are going to use this variable to determine how long it takes for grass to regrow once it has been eaten.

  3. Add the following code to the go procedure, once the ask turtles block has finished (after the closing ']'): ask patches[
     if pcolor = brown [
      set time-since-eaten time-since-eaten + 1
      if time-since-eaten > grass-regrow-time [
       set pcolor green
      ]
     ]
    ]
    (see the right image if you're not sure where the code should go).
  4. It might look complicated at first, but the code above doesn't do anything that you haven't seen before. Its overall job is to see if enough time has passed for brown patches to become green again. The following table explains what each line does:

    CodeExplanation
    ask patches [ This tells NetLogo to run through the patches, one by one, so we can see if they are ready to re-grow.
    if pcolor = brown [ This checks the colour of an individual patch. If the patch is brown then it might be time to regrow. If it is green, then we just ignore it.
    set time-since-eaten time-since-eaten + 1 This increases the value of the time-since-eaten variable by one unit. This is how we keep track of the amount of time that has passed since the patch became brown (i.e. the grass was eaten).
    if time-since-eaten > grass-regrow-time [ Here, we make use of the grass-regrow-time slider. If enough time has passed since the patch become brown, the we can do something to it.
    set pcolor green This simply changes the colour of the patch from brown to green. Importantly, the line above makes sure that this will only happen if enough time has passed. If enough time hasn't passed, then the patch will stay brown (for now).
      ] This finishes the if time-since-eaten > grass-regrow-time [ block.
     ] This finishes the if pcolor = brown [ block.
    ] This finishes the ask patches [ block.

    Understanding the logical flow of the program as it compares variables and decides what to do is a common task in computer programming and a very useful skill in general. If you're not sure about how the if commands work, have a look back at the lecture .

  5. Finally, go back to the Interface tab and try to run the model. You should see grass be eaten and then re-grow as the turtles move around the environment.

Now you can move on to the next part where we will allow the turtles to give birth.

Practical 3, part 3 - Giving Birth


Like the last section, this part is quite short. All that you need to do is add a few lines so that if the turtles have enough energy then they can give birth.

    Code to give birth
  1. Add the following code to the go procedure, after the turtle has decided whether or not a patch of grass is good to eat: if energy > 50 [
     set energy 10
     hatch 1
    ]
  2. That's it! The code above checks to see if the turtle has more than 50 units of energy stored up. If it does, then we reduce the amount of energy to 10 (to simulate the stress of giving birth) and then use the hatch command to create a copy of the turtle.

  3. Go back to the Interface tab and run the model. If you have it going slowly enough you might be able to see new turtles being born. Try experimenting with the grass-regrow-time slider. If you change the amount of time it takes for grass to re-grow, does this have an impact on the number of turtles in the model?

You'll probably notice that it is quite hard to work out how many turtles are actually in the model at any given time. It would be better if there was a way to see exactly how many turtles are alive at any given time. This is something that will be addressed next.

Practical 3, part 4 - Creating a Graph


A graph

In the very last part of this practical, you will create a graph that shows the number of turtles alive on the y axis and the number of model iterations on the x axis. This is explained in detail in the NetLogo tutorial (see the Plotting section). Most of the material here has come from that tutorial.

  1. If you have closed it, re-open your sheep model.
  2. Then go to the Interface tab and create a new 'Plot' in the same way that you would create a button, slider, etc.
  3. Enter the following values to configure how the axes are displayed:
    Name:Total Number of Alive Turtles
    X axis labeltime
    Y axis labelcount
    Auto scale:✓ (This makes the axes grow over time)
  4. Next, we need to tell NetLogo how to draw the graph. To do this, it uses something called 'Plot Pens'. It is possible to have multiple pens per graph to draw a number of different things simultaneously. For this practical, we will create a single 'pen' that draws the number of turtles who are alive at a given time.

  5. Click on 'Add pen' to make a new pen
  6. Give the pen the name turtles-alive and choose a colour for the pen (this will determine the colour of the line on the chart). In the right figure I chose orange, but the colour doesn't matter.
  7. Plot pen
  8. The 'Plot Update Commands' section is where we tell NetLogo what we actually want the chart to plot. In there, type: plot count turtles This will plot the number of turtles currently in the model.
  9. Now the graph is ready. Click on OK and you should see an empty graph.
  10. Try running the model, sadly nothing will be drawn on the graph. To make the graph work there are two very small changes that we need to make to the model.

  11. Go into the Code tab.
  12. In the setup procedure, add the following code just before creating the patches and the turtles: __clear-all-and-reset-ticks What this strange command does is remove everything from the model and reset the 'tick counter' to zero. This will be run each time you click on the 'setup' button. If we don't do this, then the graph never goes back to zero, even after the model has been reset.
    Your setup procedure should now look something like this: to setup
     print "Setting up model."
     __clear-all-and-reset-ticks
     setup-patches
     setup-turtles
    end
  13. Lastly, find the go procedure. At the very end, just before the end command (that tells NetLogo we have finished with the go procedure add the single command: tick This makes NetLogo increment it's internal clock so we know how many iterations have passed since the model began running. The plot needs to know this.

That's it! Now go back to the Interface tab and try to run the model. Does the graph work? Experiment with the grass-regrow-time slider again. Does the graph make it easier to see what affect the slider has on the size of the turtle population?

That's everything for Practical 3. You now have a reasonably advanced model with animals that eat grass, give birth, and die. To finish off, have a go at the activities below.

Activities

  1. Add a plot pen that will show the number of green patches on the graph. If you get stuck, refer to the 'Plotting' section of the NetLogo tutorial.
  2. NetLogo has lots of different items that can provide information about a model. We have seen how graphs work; other items include monitors to display numbers (e.g. counts of things) and labels to show information about individual turtles or patches. The NetLogo tutorial has details about how to create these elements.

  3. Create two monitors: one to show how many turtles there are in the model and another to show how many patches are green. Hint: you can do this without adding anything to the 'Code' tab. Refer to the tutorial if you get stuck.
Example of using monitors